package com.pipe.util;

import java.math.BigInteger;

/**
 * 整数转换器
 *
 * byte[] 转换
 * 采用小端顺序，即对于0x11223344，byte[4]保存0x11，byte[3]保存0x22，byte[2]保存0x33，byte[1]保存0x44
 *
 * @author Kiven
 */
public class IntegerConveter {

    /** int转换为byte[4]数组 */
    public static byte[] getByte4Array(int i) {
        byte[] b = new byte[4];
        b[3] = (byte) ((i & 0xff000000) >> 24);
        b[2] = (byte) ((i & 0x00ff0000) >> 16);
        b[1] = (byte) ((i & 0x0000ff00) >> 8);
        b[0] = (byte) (i & 0x000000ff);
        return b;
    }

    /** int转换为byte[3]数组 */
    public static byte[] getByte3ArrayFromInt(int i) {
        byte[] b = new byte[3];
        b[2] = (byte) ((i & 0x00ff0000) >> 16);
        b[1] = (byte) ((i & 0x0000ff00) >> 8);
        b[0] = (byte) (i & 0x000000ff);
        return b;
    }

    /** short转换为byte[2]数组 */
    public static byte[] getByte2ArrayFromShort(short i) {
        byte[] b = new byte[2];
        b[1] = (byte) ((i & 0x0000ff00) >> 8);
        b[0] = (byte) (i & 0x000000ff);
        return b;
    }

    /** 从byte数组获得一个long */
    public static long getLongFromByte(byte[] bytes) {
        String byteStr = new String();

        for (int i = 0; i < bytes.length; ++i) {
            byteStr = byteToBit(bytes[i]) + byteStr;
        }

        return new BigInteger(byteStr, 2).longValue();
    }

    /** 将16进制byte数组拆分成二进制byte数组 */
    public static byte[] getBitArrayFromByteArr(byte[] bytes) {
        byte[] bitArr = new byte[bytes.length * 8];
        int index = 0;
        for (int i = 0; i < bytes.length; i++) {
            byte[] b = getBitArrayFromByte(bytes[i]);
            System.arraycopy(b, 0, bitArr, index, 8);
            index = index + 8;
        }
        return bitArr;
    }

    /** 将二进制转换成long类型 大端 */
    public static long getLongFromBitArr(byte[] bytes){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(bytes[i]);
        }
        return Long.valueOf(sb.toString(), 2);
    }

    /** 大端 */
    public static byte[] getBitArrayFromByte(byte b) {
        byte[] bits = new byte[8];
        bits[0] = (byte) ((b >> 7) & 0x1);
        bits[1] = (byte) ((b >> 6) & 0x1);
        bits[2] = (byte) ((b >> 5) & 0x1);
        bits[3] = (byte) ((b >> 4) & 0x1);
        bits[4] = (byte) ((b >> 3) & 0x1);
        bits[5] = (byte) ((b >> 2) & 0x1);
        bits[6] = (byte) ((b >> 1) & 0x1);
        bits[7] = (byte) ((b >> 0) & 0x1);
        return bits;
    }

    /** 从byte数组的index处的连续4个字节获得一个int */
    public static int getInt(byte[] arr) {
        return (0xff000000 & (arr[3] << 24)) |
                (0x00ff0000 & (arr[2] << 16)) |
                (0x0000ff00 & (arr[1] << 8)) |
                (0x000000ff & arr[0]);
    }

    /** 从长度为4的byte数组获得一个int */
    public static int getIntFromByte4(byte[] arr) {
        return (0xff000000 & (arr[3] << 24)) |
                (0x00ff0000 & (arr[2] << 16)) |
                (0x0000ff00 & (arr[1] << 8)) |
                (0x000000ff & arr[0]);
    }

    /** 从长度为3的byte数组获得一个int */
    public static int getIntFromByte3(byte[] arr) {
        return (0x00ff0000 & (arr[2] << 16)) |
                (0x0000ff00 & (arr[1] << 8)) |
                (0x000000ff & arr[0]);
    }

    /** 从长度为2的byte数组获得一个int */
    public static int getIntFromByte2(byte[] arr) {
        return (0x0000ff00 & (arr[1] << 8)) |
                (0x000000ff & arr[0]);
    }

    /**
     * Byte转Bit
     */
    public static String byteToBit(byte b) {
        return "" + (byte) ((b >> 7) & 0x1) +
                (byte) ((b >> 6) & 0x1) +
                (byte) ((b >> 5) & 0x1) +
                (byte) ((b >> 4) & 0x1) +
                (byte) ((b >> 3) & 0x1) +
                (byte) ((b >> 2) & 0x1) +
                (byte) ((b >> 1) & 0x1) +
                (byte) ((b >> 0) & 0x1);
    }

    public static String byteArrayToBit(byte[] b) {
        String bit = "";
        for (byte bt : b) {
            bit = byteToBit(bt) + bit;
        }
        return bit;
    }
}